﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace P2P.Client.Tasks
{
    public class TimerTask
    {
        private bool isTaskExecuting = false;
        private Timer _timer;
        private Action _task;

        public TimerTask(Action task,TimeSpan interval) 
        {
            _task = task;
            _timer = new Timer(ExecuteTask, null, TimeSpan.Zero,interval);
        }

        private void ExecuteTask(object state)
        {
            if (!isTaskExecuting) 
            {
                isTaskExecuting = true;
                _task();
                isTaskExecuting = false;
            }
           
        }

        public void Dispose()
        {
            _timer.Dispose();
        }
    }
}
